package com.zhangfb95.crosschain.infra.controller.starter.httplog;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.MDC;

/**
 * @author zhangfb
 */
public class Util {

    private static final String API_URL = "_API_URL";
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static String toJsonString(Object obj) {
        String str;
        try {
            str = objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return str;
    }

    public static String getApiUrl() {
        return MDC.get(API_URL);
    }

    public static void setApiUrl(String value) {
        MDC.put(API_URL, value);
    }

    public static void removeApiUrl() {
        MDC.remove(API_URL);
    }

    public static void set(String key, String value) {
        MDC.put(key, value);
    }

    public static void remove(String key) {
        MDC.remove(key);
    }
}
